from pathlib import Path
import time
import logging

import scrapy
from scrapy_splash import SplashRequest
from stock_scrapy.items import StockScrapyItem


class HKexscSpider(scrapy.Spider):
    name = "exsc"
    allowed_domains = ["sse.com.cn"]
    start_urls = ["http://www.sse.com.cn/services/hkexsc/home/"]

    def start_requests(self):
        for url in self.start_urls:
            yield SplashRequest(url=url, callback=self.parse, args={"wait": 20})

    def is_trade_day(self, response):
        """
        Check if today is a trading day
        """

        is_trade = response.css("#js_chartId1_new::text").get()

        date_today = time.strftime("%Y-%m-%d", time.gmtime(time.time()))
        # check the scrapied date`
        # self.log("date trade: " + response.css("div.table_tip::text")[0].get(),level=logging.INFO)
        date_trade_list = response.css("div.table_tip::text")[0].re(
            "^日期：(\d{4}-\d{2}-\d{2})"
        )
        if not date_trade_list:
            self.log("No trading date found", level=logging.INFO)
            raise IOError("No trading date found")
        else:
            date_trade = date_trade_list[0]

        if is_trade == "是" and date_today == date_trade:
            return True
        else:
            return False

    def parse(self, response):
        """

        filename = f"quotes.html"
        Path(filename).write_bytes(response.body)
        self.log(f"Saved file {filename}")

        """

        # is today a trading day
        if self.is_trade_day(response):
            item = StockScrapyItem()
            item["date_time"] = time.strftime("%Y-%m-%d", time.gmtime(time.time()))
            item["could_buy"] = response.css("#js_chartId2_new::text").get() == "接受"
            item["could_sell"] = response.css("#js_chartId3_new::text").get() == "接受"
            self.log(response.css("span.rateBottom::text").getall(), level=logging.INFO)
            # get the buy and sell rates
            item["report_buy_rate"],item["report_sell_rate"],item["settle_buy_rate"],item["settle_sell_rate"] = response.css("span.rateBottom::text").getall()
            item["settle_date_time"] = response.css("div.table_tip::text")[-1].re("适用日期：(\d{4}-\d{2}-\d{2})")[0]
            # log
            self.log(f"Today is a trading day: {item['date_time']}", level=logging.INFO)

            yield item
        else:
            self.log("Today is not a trading day", level=logging.INFO)
            return
